<!--
 * @Descripttion: 文件说明
 * @version: 0.0.1
 * @Author: zhangmeng
 * @Date: 2022-10-09 15:55:09
 * @LastEditors: zhangmeng
 * @LastEditTime: 2022-10-10 14:26:37
-->
<template>
  <div class="search-form" :class="{ focused: !focused }" @mouseleave="setBlur">
    <el-input
      ref="search"
      style="width: 100%"
      size="medium"
      placeholder="关键词搜索"
      suffix-icon="el-icon-search"
      v-model="search"
      @input="searchSubmit"
      @focus="focused = true"
    >
    </el-input>
    <div v-if="focused" class="search-input-wrap">
      <div class="history">
        <div class="search-title">
          <span>最近搜索</span>
          <span class="clear" @click="clear">清空</span>
        </div>
        <div class="history-content">
          <div
            class="history-item"
            v-for="(item, index) in history"
            :key="index"
          >
            <div class="history-value" @click="sethistory(item.title)">
              {{ item.title }}
            </div>
            <div class="history-close" @click="close(item)">
              <svg-icon class="svg-icon" icon="close"></svg-icon>
            </div>
          </div>
        </div>
      </div>
      <div class="search" v-if="search != ''">
        <div class="search-item" v-for="(item, index) in 50" :key="index">
          {{ search }}---{{ index }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SearchInput',
  data() {
    return {
      search: '',
      list: [], // 搜索记录
      focused: false,
      history: [{
        id: 1,
        title: '运营看板'
      }, {
        id: 2,
        title: '公司看板'
      }, {
        id: 3,
        title: '安全'
      }, {
        id: 4,
        title: '公司看板测试1'
      }, {
        id: 5,
        title: '公司看板测试233333333'
      }, {
        id: 6,
        title: '公司看板测试4'
      }]
    }
  },
  methods: {
    sethistory(val) {
      this.search = val
    },
    searchSubmit(val) {
      console.log('搜索请求')
      this.$emit('search', val)
    },
    setBlur() {
      this.$refs.search.blur()
      this.focused = false;
    },
    clear() {
      this.history = []
    },
    close(data) {
      const findIndex = this.history.findIndex(item => { return item.id == data.id })
      this.history.splice(findIndex, 1)
    },
  }
}
</script>

<style lang="scss" scoped>
.search-form {
  position: relative;
  min-width: 360px;
  width: 100%;
  height: 36px;
}

.search-input-wrap {
  position: absolute;
  left: 0px;
  z-index: 998;
  width: 100%;
  min-height: 246px;
  max-height: 512px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border-color);
  border-top: none;
  border-radius: 0 0 4px 4px;
  padding: 14px 16px 16px 16px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 10%);

  .search {
    width: 100%;
    max-height: 300px;
    overflow: hidden;
    overflow-y: auto;
    border-top: 1px solid var(--border-color);

    .search-item {
      font-size: 14px;
      line-height: 30px;
      height: 30px;

      &:hover {
        background: #f6f7f8;
      }
    }
  }

  .history {
    width: 100%;

    .history-content {
      width: 100%;

      .history-item {
        position: relative;
        display: inline-block;
        box-sizing: border-box;
        height: 30px;
        padding: 0 6px;
        font-size: 12px;
        line-height: 30px;
        background: #f6f7f8;
        border-radius: 4px;
        margin-right: 10px;
        margin-bottom: 10px;
        cursor: pointer;

        .history-value {
          display: inline-block;
          max-width: 100px;
          height: 30px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }

        .history-close {
          position: absolute;
          top: -4px;
          right: -4px;
          background: rgba(0, 0, 0, 0.1);
          width: 10px;
          height: 10px;
          display: flex;
          justify-content: center;
          align-items: center;
          opacity: 0;
          border-radius: 50%;

          .svg-icon {
            width: 8px;
            height: 8px;
          }

          &:hover {
            color: #fff;
          }
        }

        &:hover {
          color: var(--font-hover);

          .history-close {
            opacity: 1;
          }
        }
      }
    }

    .search-title {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 6px;

      & span {
        font-size: 16px;
        color: var(--font-color);
      }

      .clear {
        cursor: pointer;
        font-size: 12px;

        &:hover {
          color: var(--font-hover);
        }
      }
    }
  }
}

.focused {
  ::v-deep {
    .el-input__inner {
      background-color: #f5f5f5 !important;
    }
  }
}
</style>